//
//  TCLYSettingViewController.m
//  YuErmuYing
//
//  Created by LYang on 16/8/22.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//

#import "TCLYSettingViewController.h"
#import "TCLYSettingTableViewCell.h"
#import "TCLYInformationViewController.h"
#import "TCLYPasswordViewController.h"
#import "TCLYAddressViewController.h"
#import "TCLYMineModel.h"
#import "Macros.h"
#import "Masonry.h"
#import "TCLYLoginViewController.h"

@interface TCLYSettingViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) TCLYMineModel * mineModel;
@property (nonatomic, strong) UITableView * settingTableView;
@property (nonatomic, strong) TCLYLoginViewController * loginViewCtr;

@end

@implementation TCLYSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = bgColor;
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.backgroundColor = statusColor;
    self.navigationController.navigationBar.barStyle = UIBarMetricsCompactPrompt;
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, -20, screenWidth, 20)];
    view.backgroundColor = navColor;
    [self.navigationController.navigationBar addSubview:view];
    
    UILabel * titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"设置";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.frame = CGRectMake(0, 0, 50, 20);
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    self.navigationItem.titleView = titleLabel;
    
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(12, 22, 20, 20);
    [leftBtn setImage:[UIImage imageNamed:@"left"] forState:UIControlStateNormal];
    leftBtn.tag = 91;
    [leftBtn addTarget:self action:@selector(statusBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    
    self.mineModel = [[TCLYMineModel alloc] init];
    [self.mineModel loadCellLabelText];
    // Do any additional setup after loading the view.
    [self createView];
}
-(void) createView {
    self.settingTableView = [[UITableView alloc] init];
    self.settingTableView.backgroundColor = bgColor;
    [self.settingTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.settingTableView.delegate = self;
    self.settingTableView.dataSource = self;
    [self.view addSubview:self.settingTableView];
    [self.settingTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-100);
    }];
    UIButton * exitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [exitBtn setTitle:@"退出登录" forState:UIControlStateNormal];
    [exitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    exitBtn.backgroundColor = btnColor;
    exitBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [exitBtn addTarget:self action:@selector(exitBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:exitBtn];
    [exitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.settingTableView.mas_bottom).offset(0);
        make.left.mas_equalTo(12);
        make.right.mas_equalTo(-12);
        make.height.mas_equalTo(44);
    }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.mineModel.settingArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * identifier = @"settingCell";
    TCLYSettingTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (!cell) {
        cell = [[TCLYSettingTableViewCell alloc] initWithReuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.settingLabel.text = self.mineModel.settingArr[indexPath.row];
    return cell;
}
//点击cell触发事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:{
            TCLYInformationViewController * informationViewCtr = [[TCLYInformationViewController alloc] init];
            [self.navigationController pushViewController:informationViewCtr animated:YES];
        }
            break;
        case 1:{
            TCLYPasswordViewController * passwordViewCtr = [[TCLYPasswordViewController alloc] init];
            [self.navigationController pushViewController:passwordViewCtr animated:YES];
        }
            break;
        case 2:{
            TCLYAddressViewController * addressViewCtr = [[TCLYAddressViewController alloc] init];
            [self.navigationController pushViewController:addressViewCtr animated:YES];
        }
            break;
    }
}

-(void)statusBtnAction:(id)sender{
    switch (((UIButton *)sender).tag) {
        case 91:{
            [self.navigationController popViewControllerAnimated:YES];
            self.navigationController.navigationBarHidden = YES;
            self.tabBarController.tabBar.hidden = NO;
        }
            break;
        case 92:{
            
        }
            break;
    }
}

//每个cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
-(void)login{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logins) name:@"login" object:nil];
    self.loginViewCtr = [[TCLYLoginViewController alloc] init];
    [self presentViewController:self.loginViewCtr animated:YES completion:nil];
}
-(void)logins{
    [self.loginViewCtr dismissViewControllerAnimated:YES completion:nil];
}

//退出按钮的点击事件
-(void)exitBtnAction{
    NSFileManager* fileManager=[NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    
    //文件名
    NSString *uniquePath=[[paths objectAtIndex:0] stringByAppendingPathComponent:@"user.md5"];
    BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:uniquePath];
    if (!blHave) {
        NSLog(@"no  have");
        return ;
    }else {
        NSLog(@" have");
        BOOL blDele= [fileManager removeItemAtPath:uniquePath error:nil];
        if (blDele) {
            NSLog(@"dele success");
        }else {
            NSLog(@"dele fail");
        }        
    }
    [self login];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
